package com.bsj.travel.monitor.service.impl;

import com.bsj.travel.constant.Constants;
import com.bsj.travel.constant.NumericalConstants;
import com.bsj.travel.constant.TdEngineDataConstants;
import com.bsj.travel.def.common.DTO.web.monitor.SysLogDTO;
import com.bsj.travel.def.global.JsonResult;
import com.bsj.travel.def.monitor.CloudLog;
import com.bsj.travel.mapper.tdengine.TableTDengineMapper;
import com.bsj.travel.monitor.service.SysLogService;
import com.bsj.travel.security.auth.AuthProduct;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yinhao
 * @version 1.0
 * @description 日志业务实现类
 * @date 2024/1/26
 */
@Service
@Slf4j
public class SysLogServiceImpl implements SysLogService {

    @Resource
    private AuthProduct authProduct;

    @Resource
    private TableTDengineMapper tableTDengineMapper;

    /**
     * 查询日志信息
     *
     * @param sysLog
     * @return
     */
    @Override
    public JsonResult<List<CloudLog>> selectLogPageList(SysLogDTO sysLog) {
        String productKey = sysLog.getProductKey();
        //验证产品权限
        authProduct.permissionCheck(productKey);
        sysLog.setTableName(TdEngineDataConstants.T_LOG + productKey);
        String deviceName = sysLog.getDeviceName();
        if(StringUtils.isNotBlank(deviceName)){
            sysLog.setEvent(deviceName + Constants.AND_SYMBOL + productKey);
        }
        int total = tableTDengineMapper.countLog(sysLog);
        if (total == NumericalConstants.ZERO) {
            return JsonResult.success(new ArrayList<>(), total);
        }
        sysLog.initPageNumber();
        return JsonResult.success(tableTDengineMapper.pageLog(sysLog), total);
    }
}
